import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  UseInterceptors,
  UploadedFile,
  UploadedFiles,
  Session,
  Req,
} from '@nestjs/common';
import { FileInterceptor, FilesInterceptor } from '@nestjs/platform-express';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiConsumes,
} from '@nestjs/swagger';
import { Request } from 'express';
import { JwtAuthGuard } from '@wwjCommon/security/guards/jwt-auth.guard';
import { RolesGuard } from '@wwjCommon/security/guards/roles.guard';
import { Roles } from '@wwjCommon/security/decorators/roles.decorator';
import { Public } from '@wwjCommon/security/decorators/public.decorator';
import { BusinessException } from '@wwjCommon/exception/business.exception';
// @UploadedFile() - 单文件上传，配合 @UseInterceptors(FileInterceptor('file'))
// @UploadedFiles() - 多文件上传，配合 @UseInterceptors(FilesInterceptor('files'))
// @Session() - 获取会话对象，对应PHP Session::get()
// @Req() - 获取Request对象，对应PHP Request
import { AttachmentService } from '../../services/admin/attachment.service';

/**
 * AttachmentController
 * 对应 PHP: Attachment Controller
 * 对应 Java: @RestController
 *
 * 支持装饰器：
 * - @UploadedFile() - 单文件上传 (对应PHP Request::file())
 * - @UploadedFiles() - 多文件上传
 * - @Session() - 会话管理 (对应PHP Session::get())
 * - @Req() - 请求对象 (对应PHP Request)
 */
@ApiTags('sys')
@Controller('adminapi/sys')
export class AttachmentController {
  constructor(private readonly attachmentService: AttachmentService) {}

  /**
   * 附件管理
   * 路由: GET attachment
   * PHP路由: Route::get('attachment', 'sys.Attachment/lists')
   */
  @Get('attachment')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '附件管理' })
  async lists(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: lists

      return await this.attachmentService.getPage(data);
    } catch (error) {
      throw new BusinessException('lists操作失败', error);
    }
  }

  /**
   * 附件管理
   * 路由: DELETE attachment/del
   * PHP路由: Route::delete('attachment/del', 'sys.Attachment/batchDel')
   */
  @Delete('attachment/del')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '附件管理' })
  async batchDel(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: batchDel

      return await this.attachmentService.delAll(data['att_ids']);
    } catch (error) {
      throw new BusinessException('batchDel操作失败', error);
    }
  }

  /**
   * 附件管理
   * 路由: POST attachment/category
   * PHP路由: Route::post('attachment/category', 'sys.Attachment/addCategory')
   */
  @Post('attachment/category')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '附件管理' })
  async addCategory(@Body() data: AddCategoryDto): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: addCategory

      return await this.attachmentService.addCategory(data);
    } catch (error) {
      throw new BusinessException('addCategory操作失败', error);
    }
  }

  /**
   * 附件管理
   * 路由: GET attachment/category
   * PHP路由: Route::get('attachment/category', 'sys.Attachment/categoryLists')
   */
  @Get('attachment/category')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '附件管理' })
  async categoryLists(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: categoryLists

      return await this.attachmentService.getCategoryList(data);
    } catch (error) {
      throw new BusinessException('categoryLists操作失败', error);
    }
  }

  /**
   * 附件管理
   * 路由: PUT attachment/category/:id
   * PHP路由: Route::put('attachment/category/:id', 'sys.Attachment/editCategory')
   */
  @Put('attachment/category/:id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '附件管理' })
  async editCategory(
    @Param('id') id: string,
    @Body() data: EditCategoryDto,
  ): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: editCategory

      return await this.attachmentService.editCategory(id, data);
    } catch (error) {
      throw new BusinessException('editCategory操作失败', error);
    }
  }

  /**
   * 附件管理
   * 路由: DELETE attachment/category/:id
   * PHP路由: Route::delete('attachment/category/:id', 'sys.Attachment/deleteCategory')
   */
  @Delete('attachment/category/:id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '附件管理' })
  async deleteCategory(@Param('id') id: string): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: deleteCategory

      return await this.attachmentService.delCategory(id);
    } catch (error) {
      throw new BusinessException('deleteCategory操作失败', error);
    }
  }

  /**
   * 附件管理
   * 路由: PUT attachment/move/:att_id
   * PHP路由: Route::put('attachment/move/:att_id', 'sys.Attachment/moveCategory')
   */
  @Put('attachment/move/:att_id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '附件管理' })
  async moveCategory(
    @Param('att_id') att_id: string,
    @Body() data: MoveCategoryDto,
  ): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: moveCategory

      return await this.attachmentService.modifyCategory(
        att_id,
        data['cate_id'],
      );
    } catch (error) {
      throw new BusinessException('moveCategory操作失败', error);
    }
  }

  /**
   * 附件管理
   * 路由: PUT attachment/batchmove
   * PHP路由: Route::put('attachment/batchmove', 'sys.Attachment/batchMoveCategory')
   */
  @Put('attachment/batchmove')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '附件管理' })
  async batchMoveCategory(
    @Body() data: BatchMoveCategoryDto,
  ): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: batchMoveCategory

      return await this.attachmentService.batchModifyCategory(
        data['att_ids'],
        data['cate_id'],
      );
    } catch (error) {
      throw new BusinessException('batchMoveCategory操作失败', error);
    }
  }
}
